package com.quasar.hpatient.model.custommodel;

import com.quasar.hpatient.utils.AlarmTime;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

public class AlarmItem implements Serializable {

    public long repeat = 0l;
    public int day = 0;
    private String uuid;
    public AlarmTime time;
    public Calendar first;

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    public AlarmItem() {
        uuid = UUID.randomUUID().toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AlarmItem alarmItem = (AlarmItem) o;

        return !(uuid != null ? !uuid.equals(alarmItem.uuid) : alarmItem.uuid != null);

    }
    public float getDosage(){
        float reslt=1.0f;
        try {
            reslt=Float.valueOf(time.unit);
        }catch (Exception e){

        }return  reslt;
    }

    @Override
    public int hashCode() {
        return uuid != null ? uuid.hashCode() : 0;
    }

    public Calendar getFirst() {
        Calendar old = time.getCalendar();
        Calendar result = Calendar.getInstance();
        result.setTime(first.getTime());
        result.set(Calendar.HOUR_OF_DAY, old.get(Calendar.HOUR_OF_DAY));
        result.set(Calendar.MINUTE, old.get(Calendar.MINUTE));
        result.set(Calendar.SECOND, 0);

        Calendar now = Calendar.getInstance();
        if (result.before(now)) {
            if (day<1){
                day=1;
            }
            result.add(Calendar.DAY_OF_MONTH, day);
        }
        return result;

    }
    public Calendar getTime() {
        Calendar old = time.getCalendar();
        Calendar result = Calendar.getInstance();
        result.setTime(first.getTime());
        result.set(Calendar.HOUR_OF_DAY, old.get(Calendar.HOUR_OF_DAY));
        result.set(Calendar.MINUTE, old.get(Calendar.MINUTE));
        result.set(Calendar.SECOND, 0);
        return result;

    }
    public Calendar getTodayTime() {
        Calendar old = time.getCalendar();
        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR,2015);
        now.set(Calendar.MONTH,9);
        now.set(Calendar.DAY_OF_MONTH,3);
        now.set(Calendar.HOUR_OF_DAY, old.get(Calendar.HOUR_OF_DAY));
        now.set(Calendar.MINUTE, old.get(Calendar.MINUTE));
        now.set(Calendar.SECOND, 0);
        return now;

    }
    public String getTimeInfo() {
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        result = format.format(getFirst().getTime());
        return result;
    }
    public String getTimeSimple() {
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        result = format.format(getFirst().getTime());
        return result;
    }
    public Calendar getFirst(Calendar first) {
        Calendar old = time.getCalendar();
        Calendar result = Calendar.getInstance();
        result.setTime(first.getTime());
        result.set(Calendar.HOUR_OF_DAY, old.get(Calendar.HOUR_OF_DAY));
        result.set(Calendar.MINUTE, old.get(Calendar.MINUTE));
        result.set(Calendar.SECOND, 0);

        Calendar now = Calendar.getInstance();
        if (result.before(now)) {
            result.add(Calendar.DAY_OF_MONTH, day);
        }
        return result;
    }
}
